package cn.lzh.controller;

import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/info")
public class InfoController {
    @GetMapping("/all") //get 获取
    public  Authentication getAuthentication(Authentication authentication){
        return authentication;
    }

    @GetMapping("/getUser") //get 获取
    public Object getUser(){
        Object principal =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println("principal= "+principal);
        if(principal == null) return null;
        if(principal instanceof UserDetails){
            return  principal;
        } else {
            return String.valueOf(principal);
        }
    }
}
